﻿



using Jint;




var engine = new Engine();

engine.SetValue("log", new Action<object>(Console.WriteLine));


engine.SetValue("cw", cw);

engine.Execute(@"
      function hello() { 
        log('Hello World');
        cw('123cw')
      };
      hello();
    ");




var add=  new Engine()
        .Execute("function add(a, b) { return a + b; }")
        .GetValue("add")
        ;

var sd= add.Invoke(1, 2); // -> 3
// 按照名称也可以
//var sd = add.Invoke("add",1, 2); // -> 3


Console.WriteLine(sd.AsNumber());




var p = new 
{
    Name = "Jet"
};

var engine1 = new Engine()
     .SetValue("p", p)
     .Execute("function add() { return p.Name }")
    ;

Console.WriteLine(engine1.Invoke("add").ToString());


Console.WriteLine("ok");



static void cw(object str)
{
    Console.WriteLine( str );
}

